package com.gxa.service.zijinservice.impl;

import com.gxa.common.R;
import com.gxa.mapper.zijinmapper.FuKuanMapper;
import com.gxa.pojo.*;
import com.gxa.service.zijinservice.FuKuanDanService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author zhaohaoc
 * @version 1.0
 * @date 2022/4/8 9:26
 */
@Service
public class FuKuanDanServiceImpl implements FuKuanDanService {

    @Autowired
    public FuKuanMapper fuKuanMapper;

    @Override
    public R selectAllGongYingShang() {
        R r = new R();
        List<GongYingShang> gongYingShangList = fuKuanMapper.selectAllGongYingShang();
        if (gongYingShangList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(gongYingShangList);
        }
        return r;
    }

    @Override
    public R selectAllZhangHu() {
        R r = new R();
        List<ZhangHu> zhangHuList = fuKuanMapper.selectAllZhangHu();
        if (zhangHuList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(zhangHuList);
        }
        return r;
    }

    @Override
    public R selectJieSuanWay() {
        R r = new R();
        List<JieSuanWay> jieSuanWayList = fuKuanMapper.selectJieSuanWay();
        if (jieSuanWayList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(jieSuanWayList);
        }
        return r;
    }

    @Override
    public R selectGouHuoDan(String startTime,String endTime,
                             String gongYingShangMingCheng,String danJuBianHao) {
        R r = new R();
        GongYingShang gongYingShang = fuKuanMapper.selectGongYingShangIdByGongYingShangMingCheng(
                gongYingShangMingCheng);
        long gongyingshangid = gongYingShang.getId();
        String gongYingShangId = String.valueOf(gongyingshangid);
        List<GouHuo> gouHuoList = fuKuanMapper.selectGouHuoDan(startTime,endTime,
                gongYingShangId,danJuBianHao);
        if (gouHuoList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(gouHuoList);
        }
        return r;
    }

    @Override
    public R selectFuKuanDan(String startTime, String endTime, String fuKuanDanBianHao,
                             String gongYingShangMingCheng, String fuKuanDanBeiZhu) {
        R r = new R();
        List<FuKuanDan> fuKuanDanList = fuKuanMapper.selectFuKuanDan(startTime,endTime,
                fuKuanDanBianHao, gongYingShangMingCheng, fuKuanDanBeiZhu);
        if (fuKuanDanList != null){
            r.setCode(200);
            r.setMsg("success");
            r.setData(fuKuanDanList);
        }
        return r;
    }

    @Override
    public R insertFuKuanDan(FuKuanDan fuKuanDan) {
        R r = new R();
        try {
            Date date = fuKuanDan.getFuKuanDanRiQi();
            SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
            String fuKuanDanRiQi = sd.format(date);

            String fuKuanDanBianHao = fuKuanDan.getFuKuanDanBianHao();

            long gongYingShangId = fuKuanDan.getGongYingShangId();

            String fuKuanDanBeiZhu = fuKuanDan.getFuKuanDanBeiZhu();

            String fuKuanDanZongJinE = String.valueOf(fuKuanDan.getFuKuanZongJinE());

            int result = fuKuanMapper.addFuKuanDan(fuKuanDanRiQi,fuKuanDanBianHao,gongYingShangId,
                    fuKuanDanZongJinE,fuKuanDanBeiZhu);

            long fuKuanDanId = fuKuanMapper.selectFuKuanDanByFuKuanBianHao(fuKuanDanBianHao);

            if (fuKuanDan.getFuKuanDanZhongJianBiaoList().size() > 0){
                for (FuKuanDanZhongJianBiao fkdj:fuKuanDan.getFuKuanDanZhongJianBiaoList()) {
                    BigDecimal fuKuanJinE = fkdj.getFuKuanJinE();
                    long jieSuanWayId = fkdj.getJieSuanWayId();
                    long zhangHuId = fkdj.getZhangHuId();
                    fuKuanMapper.addFuKuanDanInFo(fuKuanDanId,zhangHuId,jieSuanWayId,fuKuanJinE);
                }
            }
            if (fuKuanDan.getFuKuanDanGouHuoDanList().size() > 0){
                for (FuKuanDanGouHuoDan fkgh:fuKuanDan.getFuKuanDanGouHuoDanList()) {
                    long gouHuoDanId = fkgh.getGouHuoDanId();
                    fuKuanMapper.addFuKuanGouHuo(fuKuanDanId,gouHuoDanId);
                }
            }
            if(result > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public R updateFuKuanDan(FuKuanDan fuKuanDan) {
        R r = new R();
        try {
            long fuKuanDanId = fuKuanDan.getId();
            String fuKuanDanBeiZhu = fuKuanDan.getFuKuanDanBeiZhu();

            String fuKuanDanZongJinE = String.valueOf(fuKuanDan.getFuKuanZongJinE());

            int result = fuKuanMapper.updateFuKuanDan(fuKuanDanId,fuKuanDanBeiZhu,fuKuanDanZongJinE);

            List<FuKuanDanZhongJianBiao> list1 = fuKuanDan.getFuKuanDanZhongJianBiaoList();
            if (fuKuanDan.getFuKuanDanZhongJianBiaoList().size() > 0){
                for (FuKuanDanZhongJianBiao fkdj:fuKuanDan.getFuKuanDanZhongJianBiaoList()) {
                    BigDecimal fuKuanJinE = fkdj.getFuKuanJinE();
                    long jieSuanWayId = fkdj.getJieSuanWayId();
                    long zhangHuId = fkdj.getZhangHuId();
                    fuKuanMapper.updateFuKuanDanInFo(fuKuanDanId,zhangHuId,jieSuanWayId,fuKuanJinE);
                }
            }
            if (fuKuanDan.getFuKuanDanGouHuoDanList().size() > 0){
                for (FuKuanDanGouHuoDan fkgh:fuKuanDan.getFuKuanDanGouHuoDanList()) {
                    long gouHuoDanId = fkgh.getGouHuoDanId();
                    fuKuanMapper.updateFuKuanGouHuo(fuKuanDanId,gouHuoDanId);
                }
            }
            if(result > 0){
                r.setCode(200);
                r.setMsg("success");
            }
        }catch (Exception e){
            r.setCode(500);
            r.setMsg("系统开小差了，请联系管理员！");
            e.printStackTrace();
        }
        return r;
    }

    @Override
    public R delFuKuanDan(String fuKuanDanBianHao) {
        R r = new R();
        long fuKuanDanId = fuKuanMapper.selectFuKuanDanByFuKuanBianHao(fuKuanDanBianHao);
        int result =fuKuanMapper.delFuKuanDanByBianHao(fuKuanDanBianHao);
        int result1 = fuKuanMapper.delFuKuanDanInFo(fuKuanDanId);
        int result2 = fuKuanMapper.delFuKuanGouHuo(fuKuanDanId);
        if(result > 0){
            r.setCode(200);
            r.setMsg("success");
        }
        return r;
    }

}
